package com.cheng.lesson01;

import java.net.InetAddress;
import java.net.UnknownHostException;

//测试IP
public class TestInetAddress {
    public static void main(String[] args) {
        try {
            //查询本机地址
            InetAddress InetAddress1 = InetAddress.getByName("127.0.0.1");
            System.out.println(InetAddress1);///127.0.0.1
            InetAddress InetAddress4 = InetAddress.getLocalHost();
            System.out.println(InetAddress1);///127.0.0.1
            InetAddress InetAddress5 = InetAddress.getByName("localhost");
            System.out.println(InetAddress5);//localhost/127.0.0.1


            //查询网站IP地址
            InetAddress InetAddress2 = InetAddress.getByName("www.baidu.com");
            System.out.println(InetAddress2);//www.baidu.com/36.152.44.95

            //常用方法
            System.out.println(InetAddress2.getAddress());//[B@1b6d3586
            System.out.println(InetAddress2.getCanonicalHostName());//36.152.44.95
            System.out.println(InetAddress2.getHostAddress());//36.152.44.95
            System.out.println(InetAddress2.getHostName());//www.baidu.com
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}
